Skip to content

fix: restore terminal state on abnormal exit#1

Open
Ivaloz wants to merge 2 commits into
mainfrom
fix/terminal-cleanup-on-abnormal-exit
Open

fix: restore terminal state on abnormal exit#1
Ivaloz wants to merge 2 commits into
mainfrom
fix/terminal-cleanup-on-abnormal-exit

Conversation

@Ivaloz

@Ivaloz Ivaloz commented Jun 12, 2026

Copy link
Copy Markdown
Owner

问题描述

当模型返回 "Internal error" 导致进程异常退出时,终端状态未正确恢复,导致:

  1. 持续滚动:退出后鼠标移动仍触发终端滚动
  2. 会话恢复崩溃:通过 Web UI 恢复会话后输入新命令,TUI 直接闪退

根因分析

exit.tsx 中只处理了 SIGHUP 信号,没有处理 SIGINTSIGTERM。当模型错误导致进程被杀时,终端清理逻辑(raw mode、alternate screen buffer)不会执行。

修复方案

1. packages/opencode/src/cli/cmd/tui/context/exit.tsx

添加信号处理器,确保异常退出时执行清理:

process.on("SIGHUP", () => exit())
process.on("SIGINT", () => exit())    // 新增
process.on("SIGTERM", () => exit())   // 新增
process.on("beforeExit", () => exit()) // 新增

2. packages/opencode/src/cli/cmd/tui/thread.ts

在 Worker 线程中添加信号处理,确保子进程清理:

process.on("SIGINT", () => stop())
process.on("SIGTERM", () => stop())

测试

  • 正常 CTRL+C 退出,终端恢复正常
  • 模型错误后 CTRL+C 退出,终端恢复正常
  • 鼠标滚动不再异常
  • Web UI 恢复会话后输入命令不闪退

环境

  • OS: Windows 11
  • Terminal: Windows Terminal
  • OpenCode Version: 0.1.0

Bin added 2 commits June 12, 2026 10:09
Add signal handlers for SIGINT, SIGTERM, and beforeExit to ensure
terminal cleanup (raw mode, alternate screen buffer) happens even
when the process is killed by model errors or external signals.

Fixes: Terminal scrolling persists after CTRL+C exit, and session
resume crashes due to corrupted terminal state.
Sync from upstream PR XiaomiMiMo#274 (whitelonng):
- setupProjectIdEnvironment: tolerate filesystem errors on Windows
- Log rotation: cap at 20MB per file, keep 10 files max (~200MB total)
- Update cleanup glob pattern to match rotated filenames

Fixes XiaomiMiMo#243, XiaomiMiMo#209, XiaomiMiMo#219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant